' Copyright (c) Microsoft Corporation.  All rights reserved.
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Runtime.CompilerServices

Namespace EdmMetadataHelpersVB
    ''' <summary>
    ''' A set of useful extension methods
    ''' </summary>
    ''' <remarks></remarks>
    <Extension()> _
    Public Module ExtensionMethods
        ''' <summary>
        ''' Allows you to enumerate a DataReader as a Enumeration of DataRecord
        ''' </summary>
        ''' <remarks>
        ''' Because VB doesn't support yield like C# we have to actually implement the IEnumerable(Of IDataRecord) 
        ''' and IEnumerator(Of IDataRecord) directly
        ''' </remarks>
        <Extension()> _
        Public Function Enumerate(ByVal reader As IDataReader) As IEnumerable(Of IDataRecord)
            Return New DataReaderEnumerable(reader)
        End Function
        ''' <summary>
        ''' Fills the provided datatable with all the records in the Enumeration
        ''' </summary>
        ''' <remarks>
        ''' Assumes that the all the columns in the datatable are also found in the DataRecord
        ''' and have compatible types
        ''' </remarks>
        <Extension()> _
        Public Sub FillWith(ByVal table As DataTable, ByVal records As IEnumerable(Of IDataRecord))
            Dim record As IDataRecord
            For Each record In records
                Dim row As DataRow = table.NewRow
                Dim column As DataColumn
                For Each column In table.Columns
                    row.Item(column.ColumnName) = record.Item(column.ColumnName)
                Next
                table.Rows.Add(row)
            Next
        End Sub
        ''' <summary>
        ''' Does the same as String.Format(template, param string[] replaces)
        ''' except it makes it available via intellisense on the template directly
        ''' </summary>
        <Extension()> _
        Public Function Merge(ByVal template As String, ByVal ParamArray values As String()) As String
            Return String.Format(template, DirectCast(values, Object()))
        End Function

    End Module
End Namespace

